﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace 功能测试软件.BLL
{
    public class SingelTestViewBLL
    {
        public View.SingelTestView _singelTestView = null;
        private BitmapImage QrImage = null;
        private BitmapImage BlackRedImage = null;
        public Model.TestModel _testModel;
        /// <summary>
        /// 读取默认图片
        /// </summary>
        public void ReadPic()
        {
            try
            {
                QrImage = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\BaseQR.png"));
                BlackRedImage = new BitmapImage(new Uri(Environment.CurrentDirectory + "\\BaseBlackRed.png"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 初始化流程显示
        /// </summary>
        /// <param name="viewModel"></param>
        public void InitStep(ViewModel.SingelTestViewModel viewModel)
        {
            try
            {
                _singelTestView.Dispatcher.Invoke(() =>
                {
                    viewModel.Mac = "";
                    viewModel.MacStepColor = Brushes.LightGray;
                    viewModel.dBm = "";
                    viewModel.dBmStepColor = Brushes.LightGray;
                    viewModel.RgbColor = _testModel.ChannelRgbEnable ? Brushes.LightGray : Brushes.Transparent;
                    viewModel.RgbStepColor = Brushes.LightGray;
                    if (_testModel.ChannelPicEnable == true)
                    {
                        viewModel.Pic = _testModel.ChannelPicTemp == "QR" ? QrImage : BlackRedImage;
                    }
                    viewModel.PicStepColor = Brushes.LightGray;
                    viewModel.isokStepColor = Brushes.LightGray;
                    viewModel.Current = "";
                    viewModel.CurStepColor = Brushes.LightGray;
                    viewModel.Result = "Wait";
                    viewModel.ResultColor= Brushes.LightGray;
                    viewModel.ResultStepColor = Brushes.LightGray;
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 初始化显示
        /// </summary>
        /// <param name="viewModel"></param>
        public void InitView(ViewModel.SingelTestViewModel viewModel)
        {
            try
            {
                _singelTestView.Dispatcher.Invoke(() =>
                {
                    viewModel.Channel = _testModel.ChannelNo;
                    viewModel.Opacity = _testModel.ChannelEnable == true? 1:0.1;
                    viewModel.RgbColor = _testModel.ChannelRgbEnable ? Brushes.LightGray : Brushes.Transparent;
                    if (_testModel.ChannelPicEnable == true)
                    {
                        viewModel.Pic = _testModel.ChannelPicTemp == "QR" ? QrImage : BlackRedImage;
                    }
                    if (_testModel.ChannelPicEnable == false)
                    {
                        viewModel.Pic = null;
                    }
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 初始化通道串口
        /// </summary>
        public void InitPort()
        {
            try
            {
                if (_testModel.ChannelPort.IsOpen == true)
                {
                    _testModel.ChannelPort.Close();
                }
                if (_testModel.ChannelPortname != "")
                {
                    BtLabelTool.PortHelper.InitPort(_testModel.ChannelPort, _testModel.ChannelPortname, _testModel.ChannelPortBaudrate);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 打开通道串口
        /// </summary>
        public void OpenPorts()
        {
            try
            {
                if (View.TestView.config.Channel1Portname != "" && _testModel.ChannelPort.IsOpen == false)
                {
                    _testModel.ChannelPort.Open();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 异步删除文件
        /// </summary>
        /// <param name="path"></param>
        public void DeletePic(string path)
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    try
                    {
                        File.Delete(path);
                        break;
                    }
                    catch (Exception)
                    {
                        Thread.Sleep(1000);
                    }
                }
            });
        }
    }
}
